﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace filestream
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int p = comboBox1.SelectedIndex;
            if (p == -1)
            {
                MessageBox.Show("请您选择文件写入方式", "保存信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                string filecontent = richTextBox1.Text.Trim();
                MyFileOption myoption = new MyFileOption();
                string filepath = @"c:\1.txt";
                bool i = myoption.WriteTextFile(filepath, filecontent, Convert.ToInt16(comboBox1.SelectedIndex));
                if (i == true)
                {
                    MessageBox.Show("保存成功", "保存信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("写入文件错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }


            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Int16 p = Convert.ToInt16(comboBox2.SelectedIndex);
            if (p == -1)
            {
                MessageBox.Show("请您选择磁盘文件操作方式", "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                string sourcepath = "c:\\1.txt";
                string targetpath = "c:\\2.txt";
                MyFileOption myoption = new MyFileOption();
                bool i = myoption.WriteTextFile(sourcepath, targetpath, p);
                if (i == true)
                {
                    MessageBox.Show("磁盘文件操作成功", "保存信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("磁盘文件操作时出差", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

      

        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = null;
            richTextBox1.Focus();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            MyFileOption myoption = new MyFileOption();
            string filepath = @"c:\1.txt";
            Int16 i = 0;
            string filecontent = "";
            myoption.ReadTextFile(filepath, out i, out filecontent);
            if (i == 0)
            {
                MessageBox.Show(filecontent, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                richTextBox1.Text = filecontent;
            }
            else if (i == 1)
            { 
                richTextBox1.Text = filecontent;
                MessageBox.Show("读取文件成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Warning);
               
            }
            if (i == 2)
            {
                richTextBox1.Text = filecontent;
                MessageBox.Show(filecontent, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string filepath = @"c:\1.txt";
            if (checkBox1.Checked && checkBox2.Checked)
            {
                File.SetAttributes(filepath, FileAttributes.ReadOnly);
                MessageBox.Show("文件已经改为只读且隐藏", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (!checkBox1.Checked && !checkBox2.Checked)
                {
                    File.SetAttributes(filepath, FileAttributes.Archive);
                    MessageBox.Show("文件已经改为正常", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if (checkBox2.Checked)
                    {
                        File.SetAttributes(filepath, FileAttributes.ReadOnly);
                        MessageBox.Show("文件已经改为只读", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    }
                    if (checkBox1.Checked)
                    {
                        File.SetAttributes(filepath, FileAttributes.Hidden);
                        MessageBox.Show("文件已经改为隐藏", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    }
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Int16 p = Convert.ToInt16(comboBox3.SelectedIndex);
            if (p == -1)
            {
                MessageBox.Show("请您选择文件夹操作方式", "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                string sourcepath = @"c:\1";
                string targetpath = @"c:\2";
                MyFileOption myoption = new MyFileOption();
                string[] filesname = null;
                bool i = myoption.DirectoryOption(sourcepath, targetpath, p, out filesname);
                if (i == true)
                {
                    MessageBox.Show("磁盘文件夹操作成功", "保存信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (filesname != null)
                    {
                        foreach (string somestring in filesname)
                        {
                            richTextBox1.Text += somestring + "\r\n";
                        }
                    }
                }
                else
                {
                    MessageBox.Show("磁盘文件夹操作时出错", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
    }
}
